local excel_data = {}
excel_data[1] = load_excel('数值投放.xlsx','成长武器')
excel_data[2] = load_excel('数值投放.xlsx','成长衣服')

local items = {'成长武器','成长衣服'}
for a,name in ipairs(items) do
    local mt = ac.skill[name]
    mt.item_id = a
    mt.item_level = 0
    mt.max_level = 50
    mt.permanent = true
    mt.ct_tip = ''

    function mt:on_add()
        local hero = self.owner
        local player = hero.owner
        local data = player:get_data('成长装备') or {}
        data[self.item_id] = self
        player:set_data('成长装备',data)
        
        if player:is_self() then
            get_weapon_ui():set_data(data)
            get_weapon_ui():show()
        end
        self:set_item_level(0)
        self:gc(hero:event '单位-更换英雄'(function()
            self:set_item_level(self.item_level)
        end))
    end
    

    function mt:set_item_level(level)
        if level>self.max_level then
            return
        end
        local hero = self.owner
        local player = hero.owner
        local attrs = excel_data[self.item_id][level]
        local data = player:get_data('成长装备') or {}
        --属性出处理
        LoadItemSX(hero,self,-1)
        for k,v in pairs(attrs) do
            self[k] = v
        end
        self.art = attrs['图标']
        LoadItemSX(hero,self,1)
        --技能处理
        local abi_name = attrs['技能2']
        -- if hero:get_slk('weapTp1')=='missile' then
        --     abi_name = attrs['技能2'] or abi_name
        -- end
        if self.abi_skill then
            self.abi_skill:remove()
        end
        self.abi_tip = ''
        if abi_name then
            self.abi_skill = hero:add_skill(abi_name,'隐藏')
            self.abi_tip = ('|n%s:|n%s'):format(abi_name,self.abi_skill:get_tip())
        end

        self.item_level = level
        if level==self.max_level then
            self.sjtip = '|n|cffff0000已满级|r'
        elseif level%10==9 and level>1 then
            self.sjtip = ('|n|cffffcc00点击挑战升级Boss|r(%0.f金币)'):format(self['升级金币'] or 0)
        else
            self.sjtip = ('|n|cffffcc00点击升级|r(%0.f金币)'):format(self['升级金币'] or 0)
        end
        self.title = ('%s-Lv%s'):format(self.name,level)
        self.tip = [[属性：|n'atts_tip''abi_tip'|n'ct_tip'|n'sjtip']]

        --刷新ui
        if player:is_self() then
            --panel:play_animation(self.item_id)
            get_weapon_ui():set_data(data)
        end
    end
    
end

local unit_level = {1,4,8,12,15}
ac.sync:event '玩家-升级成长装备'(function(_,player,index,count)
    local hero = player.hero
    local data = player:get_data('成长装备') or {}
    local item = data[index]
    local count = count or 1
    if item and hero then
        ac.loop(100,function(t)
            count = count - 1
            if count<=0 then
                t:remove()
            end
            local gold = tonumber(item['升级金币'])
            local button = get_weapon_ui().buttons[index]
            if item.item_level>=item.max_level then
                t:remove()
                return
            end
            if player:get('金币')<gold then
                show_panel_effect(player,button,'金币')
                t:remove()
                return
            end
            player:add('金币',-gold)
            if (item.item_level+1)%10==0 and item.item_level>1 then
                local key = '装备升级'..index
                if player:get_data(key) then
                    t:remove()
                    return
                end
                player:set_data(key,true)
                hero:notify('英雄-私人挑战',hero,'专属升级',30,function(flag)
                    player:set_data(key,nil)
                    if flag then
                        show_panel_effect(player,button,'成功')
                        item:set_item_level(item.item_level + 1)
                        player:notify('玩家-专属附魔',player,item)
                    end
                end,function(unit)
                    local level = unit_level[math.ceil(item.item_level/10)]
                    unit:set_enemy_level(level)
                    player:set_data('专属Boss'..index,unit)
                end)
                
                t:remove()
                return
            else
                show_panel_effect(player,button,'成功')
                item:set_item_level(item.item_level + 1)
            end
            player:notify('玩家-刷新UI',player)
        end):on_timer()
    end
end)

ac.game:event '玩家-选择英雄完毕'(function(_,player,hero)
    hero:add_skill('成长武器','隐藏')
    hero:add_skill('成长衣服','隐藏')
end)
